<html>
<head>
<title>demo</title>
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
<meta content="yes" name="apple-mobile-web-app-capable" />
<meta content="black" name="apple-mobile-web-app-status-bar-style" />
<meta name="format-detection" content="telephone=no" />
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
    <link rel="stylesheet" href="../css/weui.css"/>
    <link rel="stylesheet" href="../css/example.css"/>
    
<script type="text/javascript" src="/shambles/mobile/singleton.js"></script>

 <script type="text/javascript">
 var redirectUrl="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+APPID+"&redirect_uri="+DOMAIN+"/shambles/mobile/1/safety/voice.html&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
</script>

<script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
</head>
<body>
<div class="wxapi_container">
    <div class="lbox_close wxapi_form">
      <button class="btn btn_primary" id="startRecord">开始</button>
      <button class="btn btn_primary" id="deleteVoice">删除</button>
      <button class="btn btn_primary" id="uploadVoice">确认</button>
    </div>
</div>

</body>
<script>
var campusId=1; //公众号id

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
   }


var code=getQueryString("code");
var state=getQueryString("state");


   $.get("/shambles/oauth/getUserInfoByOpenId.do",{
 		   campusId:campusId
 	   },function(text) {
       	  var obj = $.parseJSON(text);

       	 $("#brand").html(obj.campusName);
       	 $(".headimgUrl").attr("src",obj.headimgUrl);
     	 $(".nickName").html(obj.nickName);
       	  

 			 url=document.location.toString();

 			 $.ajax({
 			     url : "../../../wechat/sign.do",
 			     data : {
 			    	 campusId:campusId,
 			    	 url:url
 			     },
 			     async: false,
 			     type : "GET",
 			     success : function(data) {
 			    	 var ticket=JSON.parse(data);
 		       	     /*
 		     	      * 此处需要两次执行相同的函数，否则返回ture时不能执行以下函数
 		     	      */  
 		       	          
 			    	 
 			    	 wx.config({
 			    		debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
 			      		appId: ticket.appId,
 			      		timestamp: ticket.timestamp,
 			      		nonceStr: ticket.nonceStr,
 			      		signature: ticket.signature,
 			      		jsApiList : [ 'checkJsApi', 'onMenuShareTimeline',
 			                            'onMenuShareAppMessage', 'onMenuShareQQ',
 			                            'onMenuShareWeibo', 'hideMenuItems',
 			                            'showMenuItems', 'hideAllNonBaseMenuItem',
 			                            'showAllNonBaseMenuItem', 'translateVoice',
 			                            'startRecord', 'stopRecord', 'onRecordEnd',
 			                            'playVoice', 'pauseVoice', 'stopVoice',
 			                            'uploadVoice', 'downloadVoice', 'chooseImage',
 			                            'previewImage', 'uploadImage', 'downloadImage',
 			                            'getNetworkType', 'openLocation', 'getLocation',
 			                            'hideOptionMenu', 'showOptionMenu', 'closeWindow',
 			                            'scanQRCode', 'chooseWXPay',
 			                            'openProductSpecificView', 'addCard', 'chooseCard',
 			                            'openCard' ]
 			      	});
 			    	 
 			    	/*
		      	      * 此处需要两次执行相同的函数，否则回调后不能执行以下函数
		      	      */ 
 			    	 wx.ready(function () {
 			  
 			    		var voice = {
 			                    localId: '',
 			                    serverId: ''
 			                };
 			            var startRecord = document.querySelector('#startRecord');
 			            var stopClearTimeout;
 			            startRecord.onclick = function () {
 			            //开始录音
 			            if(startRecord.innerHTML == '开始'){
 			                wx.startRecord({
 			                    success: function(){
 			                        startRecord.innerHTML = '停止';
 			                        //30秒后自动停止;
 			                        stopClearTimeout = setTimeout(function(){
 			                            wx.stopRecord({
 			                                success: function (res) {
 			                                    voice.localId = res.localId;
 			                                    startRecord.innerHTML = '试听';
 			                                }
 			                            });
 			                        },30000);
 			                    },
 			                });
 			            }
 			            
 			                //结束录音
 			            if(startRecord.innerHTML =='停止'){
 			                wx.stopRecord({
 			                    success: function (res) {
 			                        clearTimeout(stopClearTimeout);
 			                        startRecord.innerHTML = '试听';
 			                        voice.localId = res.localId;
 			                    },
 			                    fail: function (res) {
 			                        alert(JSON.stringify(res));
 			                    }
 			                });
 			            }
 			    
 			            //试听音频
 			            if(startRecord.innerHTML =='试听'){
 			                if (voice.localId == '') {
 			                    alert('请先录制一段声音');
 			                    return;
 			                }
 			                wx.playVoice({
 			                    localId: voice.localId
 			                });
 			            }
 			    
 			            };
 			            
 			                //删除语音,重新录音;
 			            document.querySelector('#deleteVoice').onclick = function(){
 			                voice.localId = '';
 			                startRecord.innerHTML = '开始';
 			            }
 			            
 			                    //确认上传语音
 			            document.querySelector('#uploadVoice').onclick = function () {
 			                if (voice.localId == '') {
 			                  alert('请先录制一段声音');
 			                  return;
 			                }
 			                wx.uploadVoice({
 			                  localId: voice.localId,
 			                    success: function (res) {
 			                        voice.serverId = res.serverId;
 			                        //把微信服务器上的serverId传给后台,后台再通过serverId到微信服务器上把语音保存到自己服务器上
 			                        $.ajax({
 			                            url: '',//后台接收数据地址
 			                            dataType: "json",
 			                            type: 'get',
 			                            data: {'id': voice.serverId},
 			                            success: function (msg) {
 			                                alert('上传成功!');
 			                            },
 			                        });
 			                    }
 			                });
 			            };

		        	  
 			    	 }); //wx.ready
 			     }
 			 });
       });


</script>
</html>
</body>
</html>
